import { RouteObject } from "@/routes/interface";

/**
 * @description 获取localStorage
 * @param {String} key Storage名称
 * @return string
 */
export const localGet = (key: string) => {
	const value = window.localStorage.getItem(key);
	try {
		return JSON.parse(window.localStorage.getItem(key) as string);
	} catch (error) {
		return value;
	}
};

/**
 * @description 存储localStorage
 * @param {String} key Storage名称
 * @param {Any} value Storage值
 * @return void
 */
export const localSet = (key: string, value: any) => {
	window.localStorage.setItem(key, JSON.stringify(value));
};

/**
 * @description 清除localStorage
 * @param {String} key Storage名称
 * @return void
 */
export const localRemove = (key: string) => {
	window.localStorage.removeItem(key);
};

/**
 * @description 清除所有localStorage
 * @return void
 */
export const localClear = () => {
	window.localStorage.clear();
};

/**
 * @description 递归查询对应的路由
 * @param {String} path 当前访问地址
 * @param {Array} routes 路由列表
 * @returns array
 */
export const searchRoute = (path: string, routes: RouteObject[] = []): RouteObject => {
	let result: RouteObject = {};
	for (let item of routes) {
		if (item.path === path) return item;
		if (item.children) {
			const res = searchRoute(path, item.children);
			if (Object.keys(res).length) result = res;
		}
	}
	return result;
};

/**
 * 获取路由的path
 * @param list
 */
export const getRouterPathArray = (list: RouteObject[]) => {
	const resList: string[] = [];
	list?.forEach(item => {
		if (item.path) resList.push(item.path);
		if (item.children) {
			const res = getRouterPathArray(item.children);
			resList.push(...res);
		}
	});
	return resList;
};

/**
 * @description 获取浏览器默认语言
 * @return string
 */
export const getBrowserLang = () => {
	//@ts-ignore
	let browserLang = navigator.language ? navigator.language : navigator.browserLanguage;
	let defaultBrowserLang = "";
	if (browserLang.toLowerCase() === "cn" || browserLang.toLowerCase() === "zh" || browserLang.toLowerCase() === "zh-cn") {
		defaultBrowserLang = "zh";
	} else {
		defaultBrowserLang = "en";
	}
	return defaultBrowserLang;
};

export const isValidLngLatArray = (arr: any) => {
	// 1. 检查是否是数组
	if (!Array.isArray(arr)) return false;

	// 2. 检查是否是二维数组
	if (!arr.every(item => Array.isArray(item))) return false;

	// 3. 检查每个子数组是否有且仅有2个元素
	if (!arr.every(item => item.length === 2)) return false;

	// 4. 检查每个元素是否是有效的经纬度数字
	return arr.every(([lng, lat]) => {
		// 经度范围：-180 到 180
		const validLng = typeof lng === 'number' && lng >= -180 && lng <= 180;
		// 纬度范围：-90 到 90
		const validLat = typeof lat === 'number' && lat >= -90 && lat >= -90;
		return validLng && validLat;
	});
}

/**
 * 将 yaw（正北为0，顺时针为正，逆时针为负）统一转换为 [0, 360) 度
 * @param yaw 源角度；弧度或角度
 * @param isRadians yaw 是否为弧度，默认 true
 */
export const yawToAngle = (yaw: number, unit: "auto" | "rad" | "deg" = "auto"): number => {
	if (yaw === undefined || yaw === null || Number.isNaN(yaw as any)) return 0;

	// 转换为角度
	let deg = yaw;
	if (unit === "auto") {
		// |yaw| > ~2π 认为是角度，否则认为是弧度
		deg = Math.abs(yaw) > Math.PI * 2 ? yaw : yaw * (180 / Math.PI);
	} else if (unit === "rad") {
		deg = yaw * (180 / Math.PI);
	}

	// 输入：正东为 0，逆时针为正（到正西 180），顺时针为负（到正西 -180）
	// 输出：正北为 0，顺时针 0~360（AMap 使用）
	// 换算：航向/罗盘角 = (90 - 东向逆时针角) mod 360
	let angle = 90 - deg;
	angle = ((angle % 360) + 360) % 360;
	return angle;
};
